package com.guodi.model.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.guodi.model.utils.ExcelUtil;
import com.guodi.model.vo.ApiIdtTaskScoreExcelVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import com.guodi.core.tool.api.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;


import com.guodi.common.exception.BaseRuntimeException;
import org.springframework.web.bind.annotation.RestController;
import com.guodi.core.boot.ctrl.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
import javax.annotation.Resource;
import com.guodi.model.service.IIdtFillTaskScoreService;
import com.guodi.model.entity.IdtFillTaskScore;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
* 指标填报维度得分表 前端控制器
* @author lzy
* @date 2023-08-03
*/
@Slf4j
@Validated
@RestController
@RequestMapping("/IdtFillTaskScore")
@Api(value = "API - IdtFillTaskScoreController", tags = "指标填报维度得分表")
public class IdtFillTaskScoreController extends BaseController {

    @Resource
    private IIdtFillTaskScoreService iIdtFillTaskScoreService;


    //定义时间格式
    private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * @描述: 根据年份和行政区划编码计算评分情况
     * @入参:
     * @出参:
     * @作者: lzy
     * @日期: 2023/8/3 16:38
     **/
    @PostMapping("/calcScoreByYearAndCode")
    @ApiOperation(value = "计算评分情况")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年份", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "divisionCode", value = "行政区划编码", required = true, dataType = "string", paramType = "query")
    })
    public R calcScoreByYearAndCode(String year, String divisionCode){
        if(StringUtils.isBlank(year) || StringUtils.isBlank(divisionCode)){
            return R.fail("年份或者行政区划编码必传");
        }
        //先删除记录再生成
        iIdtFillTaskScoreService.deleteByYearAndCode(year,divisionCode);
        iIdtFillTaskScoreService.calcScoreByYearAndCode("",year,divisionCode);
        return R.data("评估成功");
    }


    /**
     * @描述: 列表查询
     * @入参:
     * @出参:
     * @作者: lzy
     * @日期: 2023/8/3 20:15
     **/
    @PostMapping("/listByEntity")
    @ApiOperation(value = "列表查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年份", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "divisionCode", value = "行政区划编码", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "维度", required = false, dataType = "string", paramType = "query")
    })
    public R listByEntity(@ApiIgnore Page page,@ApiIgnore IdtFillTaskScore idtFillTaskScore){
        IPage<IdtFillTaskScore> iPage = this.iIdtFillTaskScoreService.listByEntity(page,idtFillTaskScore);
        return R.data(iPage);
    }

    /**
     * @描述: 导出数据
     * @入参:
     * @出参:
     * @作者: lzy
     * @日期: 2023/8/4 14:41
     **/
    @RequestMapping("/exportFile")
    @ApiOperation(value = "导出数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年份", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "divisionCode", value = "行政区划编码", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "维度", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "ids", value = "选中数据的id", required = false, dataType = "string", paramType = "query")
    })
    public void exportFile(@ApiIgnore IdtFillTaskScore idtFillTaskScore, String ids, HttpServletResponse response){

        List<IdtFillTaskScore> list = this.iIdtFillTaskScoreService.lists(idtFillTaskScore,ids);

        List<ApiIdtTaskScoreExcelVo> excelVos = new ArrayList<>();
        int i = 0;
        for(IdtFillTaskScore score : list){
            i++;
            ApiIdtTaskScoreExcelVo vo = new ApiIdtTaskScoreExcelVo();
            vo.setXh(i);
            vo.setName(score.getDivisionName());
            vo.setYear(score.getYear());
            vo.setType(score.getType());
            vo.setFoumal(score.getFormula());
            vo.setScore(score.getScore());
            vo.setCreateTime(SDF.format(score.getCreateTime()));
            excelVos.add(vo);
        }

        ExcelUtil.exportExcel(excelVos, null, "评分情况表", ApiIdtTaskScoreExcelVo.class,"评分情况表.xls"
                ,response);
    }


    /**
     * @描述: 根据年份和行政区编码查询列表
     * @入参:
     * @出参:
     * @作者: lzy
     * @日期: 2023/8/3 20:15
     **/
    @PostMapping("/listByYearAndCode")
    @ApiOperation(value = "根据年份和行政区编码查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年份", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "divisionCode", value = "行政区划编码", required = true, dataType = "string", paramType = "query")
    })
    public R listByYearAndCode(String year, String divisionCode){
        if(StringUtils.isBlank(year) || StringUtils.isBlank(divisionCode)){
            return R.fail("年份或行政区划不可为空");
        }
        LambdaQueryWrapper<IdtFillTaskScore> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(IdtFillTaskScore::getYear,year);
        wrapper.eq(IdtFillTaskScore::getDivisionCode,divisionCode);
        wrapper.orderByAsc(IdtFillTaskScore::getType);
        List<IdtFillTaskScore> list = this.iIdtFillTaskScoreService.list(wrapper);
        return R.data(list);
    }


}
